package com.xy.blog.core.springmvc.result.page;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.*;

import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/3
 */
@Getter
@Setter
//@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class PageResp<T> {
    /**
     * 当前页数
     */
    private Integer pageNo;
    /**
     * 每页显示多少
     */
    private Integer pageSize;
    /**
     * 一共有多少页
     */
    private Integer pages;
    /**
     * 是否有前一页
     */
    private boolean hasPrev = false;
    /**
     * 是否有下一页
     */
    private boolean hasNext = false;


    private List<T> list;
    private Long total;


    public static<T> PageResp<T> data(List<T> data) {
        PageInfo<T> pageInfo = new PageInfo<T>(data);
        PageResp<T> resp = new PageResp<>();
        resp.setList(data);
        resp.setPageNo(pageInfo.getPageNum());
        resp.setPages(pageInfo.getPages());
        resp.setPageSize(pageInfo.getPageSize());
        resp.setTotal(pageInfo.getTotal());
        resp.setHasNext(pageInfo.isHasNextPage());
        resp.setHasPrev(pageInfo.isHasPreviousPage());
        return resp;
    }

    public static<T> PageResp<T> data(List<T> data, Page page) {
        PageResp<T> resp = new PageResp<>();
        resp.setList(data);
        resp.setPageNo(page.getPageNum());
        resp.setPages(page.getPages());
        resp.setPageSize(page.getPageSize());
        resp.setTotal(page.getTotal());
        return resp;
    }



}
